package com.zhiyou.hospital.ssh.dao.impl;

import com.zhiyou.hospital.ssh.dao.IBaseDao;
import com.zhiyou.hospital.ssh.util.LogHelper;
import com.zhiyou.hospital.ssh.util.LogUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.util.ReflectionUtils;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;

/**
 * Created by zhouyanchao on 2017/10/17.
 * Descr:
 */
@Repository("baseDao")
public class BaseDaoImpl<T> implements IBaseDao<T> {

	LogUtil log = LogHelper.getLogUtilInstance(BaseDaoImpl.class);

	@Autowired
	SessionFactory factory;

	public BaseDaoImpl() {

	}

	@Override
	public boolean addNewData(T obj) {
		try {
			Session session = factory.getCurrentSession();
//			session.persist(obj);

			session.merge(obj);

		}catch (Exception e){
			log.e(e);
			return false;
		}
		//返回
		return true;
	}

	@Override
	public boolean updateData(T obj) {
		try {
			Session session = factory.getCurrentSession();
			session.update(obj);
		}catch (Exception e){
			log.e(e);
			return false;
		}
		return true;
	}

	@Override
	public boolean deleteData(T obj) {
		try {
			Session session = factory.getCurrentSession();
			session.delete(obj);
		}catch (Exception e){
			log.e(e);
			return false;
		}
		return true;
	}

	@Override
	public T getById(Class<T> clazz,final int id) {
		T model = null;
		try {
			Session session = factory.getCurrentSession();
			model = session.get(clazz,id);
		}catch (Exception e){
			log.e(e);
		}
		return model;
	}


}
